package com.itheima.service.impl.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.dao.AddressBookDao;
import com.itheima.domain.AddressBook;
import com.itheima.domain.Dish;
import com.itheima.domain.DishFlavor;
import com.itheima.exception.NoDataException;
import com.itheima.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AddressBookServiceImpl extends ServiceImpl<AddressBookDao, AddressBook> implements AddressBookService {
    @Override
    public boolean delete(String ids) {
        //切割传进来的id 并遍历删除
        String[] strings = ids.split(",");
        for (int i = 0; i < strings.length; i++) {
//            String id =strings[i];
            LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AddressBook::getId,strings[i]);
            AddressBook addressBook = this.getOne(queryWrapper);
            if(addressBook==null){
                log.info("要删除的数据不存在");
                throw new NoDataException();
            }
            //同时删除口味表相关字段

            this.remove(queryWrapper);
        }
        return true;
    }

    @Override
    public boolean updateOne(AddressBook addressBook) {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getId,addressBook.getId());
        AddressBook book = this.getOne(queryWrapper);
        if(book==null){
            log.info("id不存在");
            return false;
        }
        this.remove(queryWrapper);
        this.save(addressBook);

        return true;
    }
}
